package com.example.demos.web.controller;

import com.example.demos.entity.Store;
import com.example.demos.service.StoreService;
import com.example.demos.util.CheckQueryParams;
import com.example.demos.util.PageBean;
import com.example.demos.util.PageParam;
import com.example.demos.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v2")
public class StoreController {
    @Autowired
    private StoreService storeService;
    @PostMapping("/store/add")
    public Result<Void> add(Store store){
        if(store.getEmpUuid() == null || store.getName().isEmpty()){
            return Result.bizErr("输入的字段不能为空");
        }
        log.info("新增仓库名{}，员工id{}",store.getName(),store.getEmpUuid());
        int flag = storeService.add(store);
        return flag == 0 ?Result.bizErr("输入内容不能与已有内容重复"):Result.ok("添加成功");
    }
    @GetMapping("/store/listByPage")
    public Result<PageBean<List<Store>>> listByPage(Integer page, Integer rows
    , @RequestParam(required = false) String name,@RequestParam(required = false) BigInteger empUuid){
        log.info("查询所有仓库");
        return Result.ok(storeService.select(page, rows,name,empUuid));
    }
    @GetMapping("/store/selectone")
    public Result<Store> selectone(Integer id){
        log.info("查询id为{}的员工",id);
        Store store = storeService.selectone(id);
        return Result.ok(store);
    }
    @PostMapping("//store/update")
    public Result<Void> update(Store store){
        int flag = storeService.update(store);
        return flag == 0 ?Result.bizErr("输入内容不能与已有内容重复"):Result.ok("修改成功");
    }

    @DeleteMapping("/store")
    public Result<Void> delete(Integer id){
        int i = storeService.delete(id);
        return i==1?Result.ok("删除成功"):Result.bizErr("当前仓库仍有货物，不可删除");
    }

}
